import 'package:flutter/material.dart';
import 'package:test1/bean/ItemInfo.dart';

class Widget_GridView_Builder_Page extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return Widget_GridView_Builder_State_Page();
  }
}

class Widget_GridView_Builder_State_Page
    extends State<Widget_GridView_Builder_Page> {
  var list = <ItemInfo>[];

  @override
  void initState() {
    super.initState();
    for (var i = 0; i < 10; i++) {
      list.add(ItemInfo("文本文本$i"));
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("GridView builder方式"),
        ),
        body: Container(
          height: 200.0,
          child: GridView.builder(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 20,
              crossAxisSpacing: 10,
//              childAspectRatio: 1,
            ),
            reverse: true,
            scrollDirection: Axis.horizontal,
            itemCount: list.length,
            itemBuilder: (BuildContext context, int position) {
              return ItemView(list, position);
            },
          ),
        ),
      ),
    );
  }
}

class ItemView extends StatelessWidget {
  List<ItemInfo>list;
  int position;

  ItemView(this.list, this.position) {}

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        color: Color(0xfff1f1f1),
        child: Text(list[position].title),
      ),
      onTap: () {
        print(position);
      },
    );
  }
}